package com.sskj.mine.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.PersonPresenter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_PERSON)
public class PersonActivity extends BaseActivity<PersonPresenter> {


    @BindView(R2.id.stvName)
    SuperTextView stvName;
    @BindView(R2.id.stvAccount)
    SuperTextView stvAccount;
    @BindView(R2.id.stvUid)
    SuperTextView stvUid;
    @BindView(R2.id.ivPhoto)
    ImageView ivPhoto;
    @BindView(R2.id.llPhoto)
    LinearLayout llPhoto;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private String filePath;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_person;
    }

    @Override
    public PersonPresenter getPresenter() {
        return new PersonPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(R.string.mine_personActivity1));
    }

    @Override
    protected void initEvent() {
        ClickUtil.click(stvName, () -> {
            ARouter.getInstance().build(RConfig.MINE_CHANGE_NAME).navigation();
        });
        ClickUtil.click(llPhoto, () -> {
            selectPic();
        });
    }

    private void selectPic() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .selectionMode(PictureConfig.SINGLE)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    LocalMedia media = selectList.get(0);
                    if (media.isCompressed()) {
                        filePath = media.getCompressPath();
                    } else {
                        filePath = media.getPath();
                    }
                    mPresenter.uploadHead(filePath);
                    break;
            }

        }
    }

    @Override
    public void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                updateUI();

            } else {
                userData = null;
            }
        });
    }

    private void updateUI() {
        if (!TextUtils.isEmpty(userData.getAvatar())) {
            ImageUtil.setCircleImage(HttpConfig.BASE_IMG_URL + userData.getAvatar(), ivPhoto);
        }
        stvAccount.setRightString(HideUtil.getPhoneHide(userData.getPhone()));
        stvName.setRightString(TextUtils.isEmpty(userData.getNickname()) ? "--" : userData.getNickname());
        stvUid.setRightString(userData.getAccount());
    }


    public void uploadHeadSuccess() {
        userViewModel.update();
    }

}
